﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Entity.Car_Rental;
using Data.Car_Rental;
using Entity;
using Web.Commons;
using Web.Commons.Helpers;
using Service.Rental;
namespace Car_Rental.Controllers
{
    public class ProductController : BaseController
    {
        IProductRepository iProductRepository = null;
        IProductService iProductService = null;
        IGetService<Product> iGetService = null;
        public ProductController()
        {
            iProductRepository = new ProductRepository();
            iProductService = new ProductService();
            iGetService = new ProductService();
        }

        protected override int PageSize
        {
            get
            {
                return base.PageSize;
            }
            set
            {
                base.PageSize = 7;
            }
        }
        //
        // GET: /Product/

        public ActionResult Index(int? page)
        {
            VP = new VPredication();
            PP = new PagingParam(page ?? 1, this.PageSize);
            var model = iProductRepository.GetProduct(VP, PP);
            return View(model);
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(FormCollection form)
        {
            Product entity = new Product();
            TryUpdateModel(entity);
            if (entity.IsValid && entity.Category_Extend.IsValid)
            {
                return RedirectToAction("Index");
            }
            else
            {
                //添加product异常依据
                entity.GetRuleViolations().ToList().ForEach(i =>
                {
                    ModelState.AddModelError(i.PropertyName, i.ErrorMessage);
                });
                //添加product的导航属性Category_Extend的异常依据
                entity.Category_Extend.GetRuleViolations().ToList().ForEach(i =>
                {
                    ModelState.AddModelError("Category_Extend." + i.PropertyName, i.ErrorMessage);
                });
            }
            return View();
        }


    }
}
